package com.tianqu.tms.core.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 图片移动
 * @author liugang
 *
 */
public class CopyFile {
	private static final Logger LOGGER = LoggerFactory.getLogger(CopyFile.class);
	public static final String IMG_EXT_JPG = ".jpg";
    public static boolean copy(String fileFrom, String fileTo) {  
        try {  
            FileInputStream in = new java.io.FileInputStream(fileFrom);  
            FileOutputStream out = new FileOutputStream(fileTo);  
            byte[] bt = new byte[1024];  
            int count;  
            while ((count = in.read(bt)) > 0) {  
                out.write(bt, 0, count);  
            }  
            in.close();  
            out.close();  
            return true;  
        } catch (Exception ex) {  
        	LOGGER.error("copy error", ex);
            return false;  
        }  
    } 
    public static boolean copy(File fileFrom, File fileTo) {  
        try {  
            FileInputStream in = new java.io.FileInputStream(fileFrom);  
            FileOutputStream out = new FileOutputStream(fileTo);  
            byte[] bt = new byte[1024];  
            int count;  
            while ((count = in.read(bt)) > 0) {  
                out.write(bt, 0, count);  
            }  
            in.close();  
            out.close();  
            return true;  
        } catch (Exception ex) {  
        	LOGGER.error("copy error", ex);
            return false;  
        }  
    }
    public static boolean copy2(SmbFile fileFrom, File fileTo) {  
        try {  
            SmbFileInputStream in = new SmbFileInputStream(fileFrom);  
            FileOutputStream out = new FileOutputStream(fileTo);  
            byte[] bt = new byte[1024];  
            int count;  
            while ((count = in.read(bt)) > 0) {  
                out.write(bt, 0, count);  
            }  
            in.close();  
            out.close();  
            return true;  
        } catch (Exception ex) {  
        	LOGGER.error("copy2 error", ex);
            return false;  
        }  
    }
    
	/**
	 * 使用smb方式进行图片copy
	 * @param fileFrom
	 * @param fileTo
	 * @param ToFileName
	 * @return
	 * @throws ParseException
	 */
    public static boolean copyWithSmb(String fileFrom, String fileTo,String ToFileName) throws ParseException { 
    	 	File localfile=null;
	        InputStream bis=null;
	        OutputStream bos=null;
        try {
        	
    	    SmbFile rmifile = new SmbFile(fileFrom);
        	bis=new BufferedInputStream(new SmbFileInputStream(rmifile));
        	//
            localfile=new File(fileTo+File.separator+ToFileName);
            
            bos=new BufferedOutputStream(new FileOutputStream(localfile));
            int length=rmifile.getContentLength();
            byte[] buffer=new byte[length];
            bis.read(buffer);
            bos.write(buffer);            
            bos.close();
            bis.close();  
            return true;  
        } catch (Exception ex) {  
        	LOGGER.error("copyWithSmb error", ex);
            return false;  
        }  
    } 
    
    /* 
     * Java文件操作 获取文件扩展名 
     * huangwei
     */  
        public static String getExtensionName(String filename) {   
            if ((filename != null) && (filename.length() > 0)) {   
                int dot = filename.lastIndexOf('.');   
                if ((dot >-1) && (dot < (filename.length() - 1))) {   
                    return filename.substring(dot + 1);   
                }   
            }   
            return filename;   
        }   
    /* 
     * Java文件操作 获取不带扩展名的文件名 
     * huangwei
     */  
        public static String getFileNameNoEx(String filename) {   
            if ((filename != null) && (filename.length() > 0)) {   
                int dot = filename.lastIndexOf('.');   
                if ((dot >-1) && (dot < (filename.length()))) {   
                    return filename.substring(0, dot);   
                }   
            }   
            return filename;   
        }   
}
